<style>
    .model-form .layui-form-label {
        width: 110px;
    }
    .model-form .layui-input-block {
        margin-left: 140px;
    }
    .cardU{
        background-image: url(../../../admin/assets/images/card_u.png);
        background-repeat: no-repeat;
        background-position: center;
        width: calc(100% - 142px);
        height: 150px;
        padding: 0;
    }
    .cardU img{width: 100%;height: 100%;}
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">名称：</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="name" class="layui-input" type="text" placeholder="请输入名称"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit>查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                            <div class="query-rt"></div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="driverBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
                        <div class="lay-btn-rt">
<!--                            <button id="driverBtnExport" class="layui-btn icon-btn layui-btn-danger">导出</button>-->
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="driverTable" lay-filter="driverTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="driverTableBar">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="driverForm">
    <form lay-filter="driverForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>名称</label>
                <div class="layui-input-block">
                    <input name="name" placeholder="请输入名称" type="text" class="layui-input" lay-verify="required" required/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>身份证号</label>
                <div class="layui-input-block">
                    <input name="idCard" placeholder="请输入身份证号" type="text" class="layui-input" lay-verify="required|idCard" required/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6" style="height: 170px;">
            <div class="layui-form-item">
                <label class="layui-form-label">驾驶证</label>
                <div class="layui-upload-drag cardU" id="driverLicense">
                    <!--<i class="layui-icon"></i>
                    <p>点击上传，或将文件拖拽到此处</p>-->
                    <input type="hidden" name="driverLicense" lay-verify="" data-name="驾驶证" >
                </div>
            </div>
        </div>
        <div class="layui-col-xs6" style="height: 170px;">
            <div class="layui-form-item">
                <label class="layui-form-label">身份证正面照</label>
                <div class="layui-upload-drag cardU" id="frontPhoto">
                    <!--<i class="layui-icon"></i>
                    <p>点击上传，或将文件拖拽到此处</p>-->
                    <input type="hidden" name="frontPhoto" lay-verify="" data-name="身份证正面照" >
                </div>
            </div>
        </div>
        <div class="layui-col-xs6" style="height: 170px;">
            <div class="layui-form-item">
                <label class="layui-form-label">身份证背面照</label>
                <div class="layui-upload-drag cardU" id="backPhoto">
                    <!--<i class="layui-icon"></i>
                    <p>点击上传，或将文件拖拽到此处</p>-->
                    <input type="hidden" name="backPhoto" lay-verify="" data-name="身份证背面照" >
                </div>
            </div>
        </div>
        <div class="layui-col-xs6" style="height: 170px;">
            <div class="layui-form-item">
                <label class="layui-form-label">头像</label>
                <div class="layui-upload-drag cardU" id="avatar">
                    <!--<i class="layui-icon"></i>
                    <p>点击上传，或将文件拖拽到此处</p>-->
                    <input type="hidden" name="avatar" lay-verify="" data-name="头像" >
                </div>
            </div>
        </div>

        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>手机号</label>
                <div class="layui-input-block">
                    <input name="phone" placeholder="请输入手机号" type="text" class="layui-input" lay-verify="required|mobile" required/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>是否启用</label>
                <div class="layui-input-block">
                    <input name="isDeleted" type="checkbox" lay-skin="switch" value="1" checked="false" lay-text="启用|停用"/>
                </div>
            </div>
        </div>
        <!--<div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
                <input name="sortNumber" placeholder="请输入排序" type="text" class="layui-input" />
            </div>
        </div>-->
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请输入备注" maxlength="200" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="driverFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'upload'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;

        // 渲染表格
        var insTb = table.render({
            elem: '#driverTable',
            url: config.base + 'enterprise/invoicing/admin/driver/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers', title: '序号', width: 80}
                ,{field: 'name', align:'center', title: '名称'}
                ,{field: 'idCard', align:'center', title: '身份证号'}
                ,{field: 'phone', align:'center', title: '手机号'}
                ,{field: 'createTime', align:'center', title: '创建时间'}
                ,{align: 'center', toolbar: '#driverTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);
                if(menuButton.indexOf("搜索") == -1){
                    $('.layui-form.toolbar').remove();
                }
                if(menuButton.indexOf("修改") == -1){
                    $("[lay-event='edit']").remove();
                }
                if(menuButton.indexOf("删除") == -1){
                    $("[lay-event='del']").remove();
                }
            }
        });

        // 工具条点击事件
        table.on('tool(driverTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(driverTable)', function(obj){
            table.reload('driverTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            $(".lay-query-box").find('select').val("");
            $(".lay-query-box").find('input').val("");
            var field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
        });

        // 添加按钮点击事件
        $('#driverBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '60%',
                offset: '120px',
                title: data ? '修改司机' : '添加司机',
                content: $('#driverForm').html(),
                success: function () {
                    form.verify({
                        idCard: function(value, item){ //value：表单的值、item：表单的DOM对象
                            if(value != ""){
                                var city={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江 ",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北 ",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏 ",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外 "};
                                if(!/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(value)){
                                    return '身份证号格式错误';
                                } else if (!city[value.substr(0,2)]){
                                    return '地址编码错误';
                                } else {
                                    //18位身份证需要验证最后一位校验位if(code.length == 18){
                                    value = value.split('');
                                    //∑(ai×Wi)(mod 11)//加权因子
                                    var factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
                                    //校验位
                                    var parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
                                    var sum = 0;
                                    var ai = 0;
                                    var wi = 0;
                                    for (var i = 0; i < 17; i++)
                                    {
                                        ai = value[i];
                                        wi = factor[i];
                                        sum += ai * wi;
                                    }
                                    var last = parity[sum % 11];
                                    if(parity[sum % 11] != value[17]){
                                        return '校验位错误';
                                    }
                                }
                            }
                        },
                        isOnlySn: function (value) {
                            var isOnly = true;
                            var idVal = $("form input[name='id']").val();
                            admin.req('mes/admin/salesBasisDriver/isOnly', {code:value,id:idVal}, function (res) {
                                if (res.code == 200) {
                                    isOnly = res.data;
                                }
                            }, 'GET',false);
                            if(!isOnly){
                                return "编码必须唯一"
                            }
                        },
                        mobile: [/^(1(3|4|5|7|8|9)\d{9}|[ \t]*)$/, '手机号码格式错误']
                    })

                    if(data){
                        var driverLicense = data.driverLicense;
                        if(driverLicense){
                            $("#driverLicense").html('<img src="'+ driverLicense+'" alt="上传成功后渲染" style="max-width: 196px"><input type="hidden" name="driverLicense" value="'+driverLicense+'">');
                        }
                        var frontPhoto = data.frontPhoto;
                        if(frontPhoto){
                            $("#frontPhoto").html('<img src="'+ frontPhoto+'" alt="上传成功后渲染" style="max-width: 196px"><input type="hidden" name="frontPhoto" value="'+frontPhoto+'">');
                        }
                        var backPhoto = data.backPhoto;
                        if(backPhoto){
                            $("#backPhoto").html('<img src="'+ backPhoto+'" alt="上传成功后渲染" style="max-width: 196px"><input type="hidden" name="backPhoto" value="'+backPhoto+'">');
                        }
                        var avatar = data.avatar;
                        if(avatar){
                            $("#avatar").html('<img src="'+ avatar+'" alt="上传成功后渲染" style="max-width: 196px"><input type="hidden" name="avatar" value="'+avatar+'">');
                        }
                    }

                    //拖拽上传驾驶证图片
                    upload.render({
                        elem: '#driverLicense'
                        ,url: config.upload_url + 'upload?path='+img_path
                        , auto: true
                        , acceptMime: 'image/*'
                        ,headers:{authorization:'Bearer '+config.getToken()}
                        ,done: function(res){
                            if(res.code == 200){
                                layer.msg("上传成功", {icon: 1});
                                var imagUrl=res.data.url;
                                /*if(imagUrl){
                                    imagUrl=imagUrl.substring(imagUrl.indexOf('/')+1)
                                }*/
                                $("#driverLicense").html('<img src="'+imagUrl+'" alt="上传成功后渲染" style="max-width: 196px"><input type="hidden" name="driverLicense" value="'+imagUrl+'">');
                            }else if(res.code == 502){
                                return location.replace('admin/login.html');
                            }else{
                                layer.msg(res.msg, {icon: 2});
                            }
                        }
                    });

                    //拖拽上传身份证正面图片
                    upload.render({
                        elem: '#frontPhoto'
                        ,url: config.upload_url + 'upload?path='+img_path
                        , auto: true
                        , acceptMime: 'image/*'
                        ,headers:{authorization:'Bearer '+config.getToken()}
                        ,done: function(res){
                            if(res.code == 200){
                                layer.msg("上传成功", {icon: 1});
                                var imagUrl=res.data.url;
                                /*if(imagUrl){
                                    imagUrl=imagUrl.substring(imagUrl.indexOf('/')+1)
                                }*/
                                $("#frontPhoto").html('<img src="'+imagUrl+'" alt="上传成功后渲染" style="max-width: 196px"><input type="hidden" name="frontPhoto" value="'+imagUrl+'">');
                            }else if(res.code == 502){
                                return location.replace('admin/login.html');
                            }else{
                                layer.msg(res.msg, {icon: 2});
                            }
                        }
                    });

                    //拖拽上传身份证反面图片
                    upload.render({
                        elem: '#backPhoto'
                        ,url: config.upload_url + 'upload?path='+img_path
                        , auto: true
                        , acceptMime: 'image/*'
                        ,headers:{authorization:'Bearer '+config.getToken()}
                        ,done: function(res){
                            if(res.code == 200){
                                layer.msg("上传成功", {icon: 1});
                                var imagUrl=res.data.url;
                                /*if(imagUrl){
                                    imagUrl=imagUrl.substring(imagUrl.indexOf('/')+1)
                                }*/
                                $("#backPhoto").html('<img src="'+imagUrl+'" alt="上传成功后渲染" style="max-width: 196px"><input type="hidden" name="backPhoto" value="'+imagUrl+'">');
                            }else if(res.code == 502){
                                return location.replace('admin/login.html');
                            }else{
                                layer.msg(res.msg, {icon: 2});
                            }
                        }
                    });

                    //拖拽上传头像图片
                    upload.render({
                        elem: '#avatar'
                        ,url: config.upload_url + 'upload?path='+img_path
                        , auto: true
                        , acceptMime: 'image/*'
                        ,headers:{authorization:'Bearer '+config.getToken()}
                        ,done: function(res){
                            if(res.code == 200){
                                layer.msg("上传成功", {icon: 1});
                                var imagUrl=res.data.url;
                                /*if(imagUrl){
                                    imagUrl=imagUrl.substring(imagUrl.indexOf('/')+1)
                                }*/
                                $("#avatar").html('<img src="'+imagUrl+'" alt="上传成功后渲染" style="max-width: 196px"><input type="hidden" name="avatar" value="'+imagUrl+'">');
                            }else if(res.code == 502){
                                return location.replace('admin/login.html');
                            }else{
                                layer.msg(res.msg, {icon: 2});
                            }
                        }
                    });

                    form.val('driverForm', data);

                    // 表单提交事件
                    form.on('submit(driverFormSubmit)', function (d) {
                        layer.load(2);
                        var isDeleted = $("form input[name='isDeleted']").prop("checked");
                        if(!isDeleted){
                            d.field.isDeleted=false;
                        }else{
                            d.field.isDeleted=true;
                        }
                        admin.req(data ? 'enterprise/invoicing/admin/driver/update' : 'enterprise/invoicing/admin/driver/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('driverTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/invoicing/admin/driver/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#driverBtnExport').click(function () {
            var checkRows = table.checkStatus('driverTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>